/*
 * OrderSpace.java
 *
 * Created on April 5, 2007, 7:11 AM
 *
 */

package edu.byu.isys413.actions;

import javax.servlet.http.*;
import edu.byu.isys413.group1E.data.*;
import edu.byu.isys413.group1E.handlers.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/** This class responds to a request from the web gui to order more backup space
 *for the current session's member.
 *
 * @author Travis Kulbeth
 */
public class OrderSpace implements edu.byu.isys413.web.Action {
    
    /** Creates a new instance of OrderSpace
     */
    public OrderSpace() {
    }
  
    /** Processes method creates a transaction and a backup revenue Source, then adds the revenue source to a 
     * transaction line, and adds that transaction line to the transaction. Then the transaction is sent to 
     * cart2.jsp, where it is added to the shopping cart view.  After that, the standard checkout process is followed.
     * 
     * @return String the name of the jsp to call
     * @param request contains the HttpServletRequest data
     * @param response contains the HttpServletResponse data
     * @throws java.lang.Exception to catch any errors and forward them to the ui level
     */
    public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        PrintHandler pHandler = new PrintHandler();

        TransactionDAO TXDAO = TransactionDAO.getInstance();
        TransLineDAO TLDAO = TransLineDAO.getInstance();
        //create & populate Transaction        
        TransactionBO TXBO = null;
        if(session.getAttribute("TX")==null)
        {
            TXBO = TXDAO.create();
            CustomerBO customer = (CustomerBO)session.getAttribute("customer");
            TXBO.setCustomerID(customer.getCustID());
        }else{
            TXBO = (TransactionBO)session.getAttribute("TX");
        }
        
        //create the backup revenue source from the data input on the site
        BackupBO bkBO = (BackupBO)BackupDAO.getInstance().create();
        
        //get today's date for the begin date
        Calendar cal = Calendar.getInstance();
        Date today = cal.getTime();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        String todayDate = fmt.format(today);
        //set the begin date
        bkBO.setBeginDate(todayDate);
        bkBO.setEndDate(BackupHandler.getInstance().addNewPeriod(todayDate));
        System.out.println((String)request.getParameter("bkSize"));
        bkBO.setBuSize(Double.parseDouble((String)request.getParameter("bkSize")));
        bkBO.setPrice(BackupHandler.getInstance().calcPrice(bkBO.getBuSize(),bkBO.getEndDate()));
        bkBO.setDescription("Additional Backup of " + bkBO.getBuSize() + "GB");
        
        TransLineBO TLBO = TLDAO.create();
        TLBO.setRevSrc(bkBO);
        TLBO.setTypeRS("backup");
        TLBO.calcLineAmount();
        TLBO.setTransactionID(TXBO.getTransID());
        
        TXBO.setTransDate(todayDate);
        TXBO.addTxLine(TLBO);
        TXBO.calcSubTotal();
        TXBO.calcTax();        
        TXBO.calcTotal();
        TXBO.setTransStatus("Complete");
        
        request.setAttribute("TX", TXBO);
                
        session.setAttribute("TX", TXBO);        
        return "cart2.jsp";
    }//process    
}
